-- CGUnrealObj
-- Created by huanghr Sep/7/2016
-- 创世之门格子

local PETER_ID = 118;

-- 初始化
function initCGUnrealObj(self)
    self.item:removeAllChildren();

    -- 拥有peter，peter不再出现
    if self.gridData.role_type == 5 and PetM.getMyPet(PETER_ID) then
        return;
    end

    local icon = self.gridData.icon;
    local iconPath = getLevelItemIconPath(icon);

    if self.gridData.role_type == 5 then
        iconPath = getHeroIconPath(PETER_ID);
    end

    local isFlippedX;
    if self.gridData.need_overturn then
        isFlippedX = true;
    else
        isFlippedX = false;
    end


    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            local hasDialog, info = CreationGateM.GetCurTimesDesc(self.gridData);
            if hasDialog then
                local function okFunc()
                    local pos = self.gridData.pos;
                    -- local content = info["content"];

                    -- if content ~= nil and content ~= "" then
                    --     -- 抛出发言事件
                    --     EventMgr.fire(event.AUDIENCE_TALK, { ["pos"] = pos, ["content"] = content, ["delay"] = 0.01,});
                    -- end

                    DungeonActionM.go("take_cg_unreal_bonus", self.gridData.pos);
                end

                self:openBuildingView(okFunc, nil, info["desc"], info["btn_text"], info["title"], iconPath, nil, nil, {["flipedX"] = isFlippedX});
            else
                alert(info["desc"]);
            end
        end
    end

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);

    if isFlippedX then
        self.icon:setFlippedX(true);
    end

    if type(self.gridData.offset) == "table" then
        local x = self.gridData.offset[1] or 0;
        local y = self.gridData.offset[2] or 0;

        self.icon:setPosition(x, y);
    end

    -- 呼吸动画
    if self.gridData.need_animation == 1 then
        self.needAnimation = true;
    end
end

